﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsFieldAttr
    {
        public int Add(KeRui.Model.CmsFieldAttr model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AttributeID", SqlDbType.Int, 4), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@ClassName", SqlDbType.NVarChar, 500), new SqlParameter("@AttributeName", SqlDbType.NVarChar, 50), new SqlParameter("@AttributeValue", SqlDbType.NVarChar, 500) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.ClassName;
            parameters[3].Value = model.AttributeName;
            parameters[4].Value = model.AttributeValue;
            DbHelperSQL.RunProcedure("CmsFieldAttr_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(string className)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ClassName", SqlDbType.NVarChar, 500) };
            parameters[0].Value = className;
            DbHelperSQL.RunProcedure("CmsFieldAttr_Delete", parameters, out num);
        }

        public DataTable GetClassList()
        {
            SqlParameter[] parameters = new SqlParameter[0];
            return DbHelperSQL.RunProcedure("CmsFieldAttr_GetClassList", parameters, "ds").Tables[0];
        }

        public KeRui.Model.CmsFieldAttr GetModel(int AttributeID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AttributeID", SqlDbType.Int, 4) };
            parameters[0].Value = AttributeID;
            KeRui.Model.CmsFieldAttr attr = new KeRui.Model.CmsFieldAttr();
            DataSet set = DbHelperSQL.RunProcedure("CmsFieldAttr_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["AttributeID"].ToString() != "")
                {
                    attr.AttributeID = int.Parse(set.Tables[0].Rows[0]["AttributeID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    attr.ParentID = int.Parse(set.Tables[0].Rows[0]["ParentID"].ToString());
                }
                attr.ClassName = set.Tables[0].Rows[0]["ClassName"].ToString();
                attr.AttributeName = set.Tables[0].Rows[0]["AttributeName"].ToString();
                attr.AttributeValue = set.Tables[0].Rows[0]["AttributeValue"].ToString();
                return attr;
            }
            return null;
        }

        public void Update(KeRui.Model.CmsFieldAttr model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AttributeID", SqlDbType.Int, 4), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@ClassName", SqlDbType.NVarChar, 500), new SqlParameter("@AttributeName", SqlDbType.NVarChar, 50), new SqlParameter("@AttributeValue", SqlDbType.NVarChar, 500) };
            parameters[0].Value = model.AttributeID;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.ClassName;
            parameters[3].Value = model.AttributeName;
            parameters[4].Value = model.AttributeValue;
            DbHelperSQL.RunProcedure("CmsFieldAttr_Update", parameters, out num);
        }
    }
}

